<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.udiprocess_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/module/udiprocess";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "编号",
              prop: "number",
              rules: [{
                required: true,
                message: "请输入编号",
                trigger: "blur"
              }]
            },
            {
              label: "编号类型：0-系统批号，1-自定义批号",
              prop: "numberType",
              rules: [{
                required: true,
                message: "请输入编号类型：0-系统批号，1-自定义批号",
                trigger: "blur"
              }]
            },
            {
              label: "数据是否同步上传医疗器械唯一标识数据库：0-否，1-是",
              prop: "isLibrary",
              rules: [{
                required: true,
                message: "请输入数据是否同步上传医疗器械唯一标识数据库：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "最小销售单位产品标识",
              prop: "unitMark",
              rules: [{
                required: true,
                message: "请输入最小销售单位产品标识",
                trigger: "blur"
              }]
            },
            {
              label: "医疗器械唯一标识编码体系名称：0-GSI，1-AHM，2-MA码",
              prop: "codeSystemName",
              rules: [{
                required: true,
                message: "请输入医疗器械唯一标识编码体系名称：0-GSI，1-AHM，2-MA码",
                trigger: "blur"
              }]
            },
            {
              label: "产品标识发布日期",
              prop: "unitMarkDate",
              rules: [{
                required: true,
                message: "请输入产品标识发布日期",
                trigger: "blur"
              }]
            },
            {
              label: "最小销售单元中使用单元的数量",
              prop: "useUnitQty",
              rules: [{
                required: true,
                message: "请输入最小销售单元中使用单元的数量",
                trigger: "blur"
              }]
            },
            {
              label: "使用单元产品标识",
              prop: "useUnitProductMark",
              rules: [{
                required: true,
                message: "请输入使用单元产品标识",
                trigger: "blur"
              }]
            },
            {
              label: "标识载体：0-一维码，1-二维码，2-RFID，3-其他（多个逗号隔开）",
              prop: "markCarrier",
              rules: [{
                required: true,
                message: "请输入标识载体：0-一维码，1-二维码，2-RFID，3-其他（多个逗号隔开）",
                trigger: "blur"
              }]
            },
            {
              label: "是否与注册/备案产品标识一致：0-否，1-是",
              prop: "isConsistent",
              rules: [{
                required: true,
                message: "请输入是否与注册/备案产品标识一致：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "注册/备案产品标识",
              prop: "filingProductMark",
              rules: [{
                required: true,
                message: "请输入注册/备案产品标识",
                trigger: "blur"
              }]
            },
            {
              label: "是否有本体直接标识：0-否，1-是",
              prop: "isOntology",
              rules: [{
                required: true,
                message: "请输入是否有本体直接标识：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "本体产品标识与最小销售单元产品标识是否一致：0-否，1-是",
              prop: "ontologyUnit",
              rules: [{
                required: true,
                message: "请输入本体产品标识与最小销售单元产品标识是否一致：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "本体产品标识",
              prop: "ontologyMark",
              rules: [{
                required: true,
                message: "请输入本体产品标识",
                trigger: "blur"
              }]
            },
            {
              label: "产品名称",
              prop: "productName",
              rules: [{
                required: true,
                message: "请输入产品名称",
                trigger: "blur"
              }]
            },
            {
              label: "商品名称",
              prop: "commodityName",
              rules: [{
                required: true,
                message: "请输入商品名称",
                trigger: "blur"
              }]
            },
            {
              label: "规格型号",
              prop: "productSku",
              rules: [{
                required: true,
                message: "请输入规格型号",
                trigger: "blur"
              }]
            },
            {
              label: "是否包类/组套类产品：0-否，1-是",
              prop: "isPackage",
              rules: [{
                required: true,
                message: "请输入是否包类/组套类产品：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "产品描述",
              prop: "productDesc",
              rules: [{
                required: true,
                message: "请输入产品描述",
                trigger: "blur"
              }]
            },
            {
              label: "产品货号或编号",
              prop: "productNumber",
              rules: [{
                required: true,
                message: "请输入产品货号或编号",
                trigger: "blur"
              }]
            },
            {
              label: "原器械目录代码",
              prop: "catalogCode",
              rules: [{
                required: true,
                message: "请输入原器械目录代码",
                trigger: "blur"
              }]
            },
            {
              label: "器械类别：0-器械，1-体外诊断试剂",
              prop: "instrumentType",
              rules: [{
                required: true,
                message: "请输入器械类别：0-器械，1-体外诊断试剂",
                trigger: "blur"
              }]
            },
            {
              label: "产品注册一级分类id",
              prop: "oneCategoryId",
              rules: [{
                required: true,
                message: "请输入产品注册一级分类id",
                trigger: "blur"
              }]
            },
            {
              label: "产品注册二级分类id",
              prop: "twoCategoryId",
              rules: [{
                required: true,
                message: "请输入产品注册二级分类id",
                trigger: "blur"
              }]
            },
            {
              label: "产品注册三级分类id",
              prop: "threeCategoryId",
              rules: [{
                required: true,
                message: "请输入产品注册三级分类id",
                trigger: "blur"
              }]
            },
            {
              label: "医疗器械注册人/备案人名称",
              prop: "filerName",
              rules: [{
                required: true,
                message: "请输入医疗器械注册人/备案人名称",
                trigger: "blur"
              }]
            },
            {
              label: "社会统一信用代码",
              prop: "creditCode",
              rules: [{
                required: true,
                message: "请输入社会统一信用代码",
                trigger: "blur"
              }]
            },
            {
              label: "注册证编号或备案编号",
              prop: "recordNumber",
              rules: [{
                required: true,
                message: "请输入注册证编号或备案编号",
                trigger: "blur"
              }]
            },
            {
              label: "医疗器械注册人/备案人英文名称",
              prop: "filerEnglishName",
              rules: [{
                required: true,
                message: "请输入医疗器械注册人/备案人英文名称",
                trigger: "blur"
              }]
            },
            {
              label: "医保编码",
              prop: "medicalCode",
              rules: [{
                required: true,
                message: "请输入医保编码",
                trigger: "blur"
              }]
            },
            {
              label: "产品类别：0-耗材，1-设备",
              prop: "productType",
              rules: [{
                required: true,
                message: "请输入产品类别：0-耗材，1-设备",
                trigger: "blur"
              }]
            },
            {
              label: "磁共振（MR）安全相关信息",
              prop: "magneticResonance",
              rules: [{
                required: true,
                message: "请输入磁共振（MR）安全相关信息",
                trigger: "blur"
              }]
            },
            {
              label: "是否标记为一次性使用：0-否，1-是",
              prop: "isDisposable",
              rules: [{
                required: true,
                message: "请输入是否标记为一次性使用：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "最大重复使用次数",
              prop: "repeatUseCount",
              rules: [{
                required: true,
                message: "请输入最大重复使用次数",
                trigger: "blur"
              }]
            },
            {
              label: "是否为无菌包装：0-否，1-是",
              prop: "isSterile",
              rules: [{
                required: true,
                message: "请输入是否为无菌包装：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "使用前是否需要进行灭菌：0-否，1-是",
              prop: "isSterilize",
              rules: [{
                required: true,
                message: "请输入使用前是否需要进行灭菌：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "灭菌方式",
              prop: "sterilizeMethod",
              rules: [{
                required: true,
                message: "请输入灭菌方式",
                trigger: "blur"
              }]
            },
            {
              label: "医保耗材分类编码",
              prop: "categoryNumber",
              rules: [{
                required: true,
                message: "请输入医保耗材分类编码",
                trigger: "blur"
              }]
            },
            {
              label: "品牌",
              prop: "brand",
              rules: [{
                required: true,
                message: "请输入品牌",
                trigger: "blur"
              }]
            },
            {
              label: "其他信息网址链接",
              prop: "otherInfoLink",
              rules: [{
                required: true,
                message: "请输入其他信息网址链接",
                trigger: "blur"
              }]
            },
            {
              label: "生产标识是否包含批号：0-否，1-是",
              prop: "isBatch",
              rules: [{
                required: true,
                message: "请输入生产标识是否包含批号：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "生产标识是否包含序列号：0-否，1-是",
              prop: "isSerial",
              rules: [{
                required: true,
                message: "请输入生产标识是否包含序列号：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "生产标识是否包含生产日期：0-否，1-是",
              prop: "isProductionDate",
              rules: [{
                required: true,
                message: "请输入生产标识是否包含生产日期：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "生产标识是否包含失效日期：0-否，1-是",
              prop: "isInvalidDate",
              rules: [{
                required: true,
                message: "请输入生产标识是否包含失效日期：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "包装产品标识",
              prop: "packageMark",
              rules: [{
                required: true,
                message: "请输入包装产品标识",
                trigger: "blur"
              }]
            },
            {
              label: "产品包装级别：0-本级包装内包含小一级相同产品标识的包装，1-本级包装为最小包装",
              prop: "packageLevel",
              rules: [{
                required: true,
                message: "请输入产品包装级别：0-本级包装内包含小一级相同产品标识的包装，1-本级包装为最小包装",
                trigger: "blur"
              }]
            },
            {
              label: "包装数量",
              prop: "packageQty",
              rules: [{
                required: true,
                message: "请输入包装数量",
                trigger: "blur"
              }]
            },
            {
              label: "包装内含小一级包装产品标识",
              prop: "minPackageMark",
              rules: [{
                required: true,
                message: "请输入包装内含小一级包装产品标识",
                trigger: "blur"
              }]
            },
            {
              label: "储存或操作条件：0-温度，1-压力，2-湿度",
              prop: "operateCondition",
              rules: [{
                required: true,
                message: "请输入储存或操作条件：0-温度，1-压力，2-湿度",
                trigger: "blur"
              }]
            },
            {
              label: "最低值",
              prop: "min",
              rules: [{
                required: true,
                message: "请输入最低值",
                trigger: "blur"
              }]
            },
            {
              label: "最高值",
              prop: "max",
              rules: [{
                required: true,
                message: "请输入最高值",
                trigger: "blur"
              }]
            },
            {
              label: "计量单位：0-C*，1-F*，2-MP",
              prop: "meteringUnit",
              rules: [{
                required: true,
                message: "请输入计量单位：0-C*，1-F*，2-MP",
                trigger: "blur"
              }]
            },
            {
              label: "特殊储存或操作条件：0-有，1-无",
              prop: "specialOperateCondition",
              rules: [{
                required: true,
                message: "请输入特殊储存或操作条件：0-有，1-无",
                trigger: "blur"
              }]
            },
            {
              label: "主键编号",
              prop: "primaryKey",
              rules: [{
                required: true,
                message: "请输入主键编号",
                trigger: "blur"
              }]
            },
            {
              label: "公开版本号",
              prop: "publicVersion",
              rules: [{
                required: true,
                message: "请输入公开版本号",
                trigger: "blur"
              }]
            },
            {
              label: "版本发布时间",
              prop: "versionPublishDate",
              rules: [{
                required: true,
                message: "请输入版本发布时间",
                trigger: "blur"
              }]
            },
            {
              label: "版本状态：0-新增，1-升级",
              prop: "versionStatus",
              rules: [{
                required: true,
                message: "请输入版本状态：0-新增，1-升级",
                trigger: "blur"
              }]
            },
            {
              label: "变更备注",
              prop: "versionRemark",
              rules: [{
                required: true,
                message: "请输入变更备注",
                trigger: "blur"
              }]
            },
            {
              label: "退市日期",
              prop: "delistingDate",
              rules: [{
                required: true,
                message: "请输入退市日期",
                trigger: "blur"
              }]
            },
            {
              label: "联系人邮箱",
              prop: "contactMail",
              rules: [{
                required: true,
                message: "请输入联系人邮箱",
                trigger: "blur"
              }]
            },
            {
              label: "联系人电话",
              prop: "contactPhone",
              rules: [{
                required: true,
                message: "请输入联系人电话",
                trigger: "blur"
              }]
            },
            {
              label: "联系人传真",
              prop: "contactFax",
              rules: [{
                required: true,
                message: "请输入联系人传真",
                trigger: "blur"
              }]
            },
            {
              label: "是否添加更多赋码信息：0-否，1-是",
              prop: "isAddCode",
              rules: [{
                required: true,
                message: "请输入是否添加更多赋码信息：0-否，1-是",
                trigger: "blur"
              }]
            },
            {
              label: "备案人识别代码",
              prop: "identificationCode",
              rules: [{
                required: true,
                message: "请输入备案人识别代码",
                trigger: "blur"
              }]
            },
            {
              label: "管理部门",
              prop: "manageDeptId",
              rules: [{
                required: true,
                message: "请输入管理部门",
                trigger: "blur"
              }]
            },
            {
              label: "负责人",
              prop: "manageUserId",
              rules: [{
                required: true,
                message: "请输入负责人",
                trigger: "blur"
              }]
            },
            {
              label: "规范文件id(多个逗号隔开)",
              prop: "guifanFilesIds",
              rules: [{
                required: true,
                message: "请输入规范文件id(多个逗号隔开)",
                trigger: "blur"
              }]
            },
            {
              label: "记录文件id(多个逗号隔开)",
              prop: "jiluFilesIds",
              rules: [{
                required: true,
                message: "请输入记录文件id(多个逗号隔开)",
                trigger: "blur"
              }]
            },
            {
              label: "记录保留期限",
              prop: "retentionPeriod",
              rules: [{
                required: true,
                message: "请输入记录保留期限",
                trigger: "blur"
              }]
            },
            {
              label: "记录流水编号",
              prop: "serialNumber",
              rules: [{
                required: true,
                message: "请输入记录流水编号",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.udiprocess_add, false),
          viewBtn: this.vaildData(this.permission.udiprocess_view, false),
          delBtn: this.vaildData(this.permission.udiprocess_delete, false),
          editBtn: this.vaildData(this.permission.udiprocess_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
